﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OBUCare.Core.DataAccess;
using OBUCare.Core.Models;
using OBUCare.Core.Common;
using Telerik.Web.Mvc;
using OBUCare.Web.Areas.Admin.Models;
using OBUCare.Web.Infrastructure;

namespace OBUCare.Web.Areas.Admin.Controllers
{
    public class DictTypeValuesController : Controller
    {
        private OBU_CAREContext db = new OBU_CAREContext();


        //
        // GET: /Admin/DictTypeValues/
        public ActionResult Index(int id)
        {
            ViewBag.DictTypeID = id;//对应的代码表编号
            var codetbl = db.DICT_TYPE.Find(id);
            ViewBag.Title = codetbl.TYPE_NAME;

            return View();
        }

        [GridAction]
        public ActionResult List(int dictTypeID)
        {
            var result = GetDictValueViewModels(dictTypeID);
            var temp = new GridModel(result);
            return View(temp);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult SaveEditing(int id, int dictTypeID)
        {
            try
            {
                var c = db.DICT_TYPE_VALUE.Find(id);
                if (TryUpdateModel(c))
                {
                    c.MODIFY_BY = UIUtility.GetCurrentUser().OPERATOR_NO;
                    c.MODIFY_DATE = DateTime.Now;

                    c.DICT_TYPE_ID = dictTypeID;
                    db.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Response.StatusCode = 500;
                Response.AppendHeader("message", "保存代码项出错: " + UIUtility.GetInnermostException(e).Message);
            }
            return View(new GridModel(this.GetDictValueViewModels(dictTypeID)));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult InsertEditing(int dictTypeID)
        {

            var c = new DICT_TYPE_VALUE();

            if (ModelState.IsValid && TryUpdateModel(c))
            {
                c.DICT_TYPE_ID = dictTypeID;

                c.CREATED_BY = UIUtility.GetCurrentUser().OPERATOR_NO;
                c.CREATED_DATE = DateTime.Now;
                c.MODIFY_BY = UIUtility.GetCurrentUser().OPERATOR_NO;
                c.MODIFY_DATE = DateTime.Now;
                c.STATUS = DictTypeStatus.Normal.ToInt().Value;
                //判断是否有重复项
                int? hasValue = (from code in db.DICT_TYPE_VALUE
                                 where code.DICT_TYPE_ID == dictTypeID
                                 && code.DICT_TITLE == c.DICT_TITLE
                                 select code.DICT_TITLE).Count();
                if (hasValue.HasValue && hasValue.Value > 0)
                {
                    Response.StatusCode = 500;
                    Response.AppendHeader("message", "标题已存在，请输入新的标题");
                }
                else
                {
                    db.DICT_TYPE_VALUE.Add(c);
                    db.SaveChanges();
                }
            }
            //Rebind the grid
            return View(new GridModel(GetDictValueViewModels(dictTypeID)));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult Delete(int id, int dictTypeID)
        {
            try
            {
                var context = new OBU_CAREContext();
                var item = context.DICT_TYPE_VALUE.Find(id);

                item.STATUS = DictTypeStatus.Deleted.ToInt().Value;
                context.SaveChanges();
            }
            catch (System.Exception e)
            {
                Response.StatusCode = 500;
                Response.AppendHeader("message", "删除代码项出错: " + UIUtility.GetInnermostException(e).Message);
            }

            return View(new GridModel(GetDictValueViewModels(dictTypeID)));
        }

        private IEnumerable<DictTypeValueViewModel> GetDictValueViewModels(int id)
        {
            var tbldictcodes = db.DICT_TYPE_VALUE.ToList();
            var results = from item in tbldictcodes
                          where item.DICT_TYPE_ID.Equals(id) && item.STATUS == 0
                          select item;

            var list = (from c in results
                        select new DictTypeValueViewModel(c)).ToList();


            return list;
        }
    }
}
